#include <QApplication>
#include "maincore.h"
#include <QWindow>
#include <QCommandLineParser>
#include <QHostAddress>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QApplication::setApplicationName("Remote Client");
    QApplication::setApplicationVersion("0.0.1 beta");
    QApplication::setOrganizationName("DongMK");
    QApplication::setApplicationDisplayName("eRemote Client");

    QCommandLineParser parser;
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addOption({"s",  "server address", "bind", "127.0.0.1"});
    parser.addOption({"p",  "server port", "port", "7777"});
    parser.process(a);

    QHostAddress address("127.0.0.1");
    if(parser.isSet("s"))
    {
        address.setAddress(parser.value("s"));
        if(address.protocol()!=QAbstractSocket::NetworkLayerProtocol::IPv4Protocol
            &&address.protocol()==QAbstractSocket::NetworkLayerProtocol::IPv6Protocol)
        {
            qCritical()<<"Server ip address parse error!";
            exit(-1);
        }
    }

    quint16 port = 7777;
    if(parser.isSet("p"))
    {
        bool ok;
        port = parser.value("p").toUShort(&ok);
        if(!ok)
        {
            qCritical()<<"Server parse port fatal!";
            exit(-1);
        }
    }

    MainCore mc;
    if(mc.connectToHost(address, port))
    {
        qInfo()<<QString("Connected success:%1 port:%2").arg(address.toString()).arg(port);
    }
    else
    {
        qCritical()<<QString("Connected failed:%1 port:%2").arg(address.toString()).arg(port).toLocal8Bit().data();
        exit(-1);
    }

    return a.exec();
}
